<?php
    session_start();
    include_once '../../../../controlador/php/conexion/Conexion.php';
    $con = new Conexion();
    
    $parteA = utf8_decode($_REQUEST['parteA']);
    $parteAB = utf8_decode($_REQUEST['parteAB']);
    $parteE = utf8_decode($_REQUEST['parteE']);
    $parteEB = utf8_decode($_REQUEST['parteEB']);
    $codpl = $_REQUEST['codpl'];
    $docum = $_REQUEST['docum'];
    $lista = $_POST['lista'];
    
    $n = count($lista);
    $i = 0;
    
    $insert= "insert into ciplmedico(ciplantilla,plmpartea,plmparteab,plmpartee,plmparteeb,usu_oid,paciente,plmfecha)values('".$codpl."','".$parteA."','".$parteAB."','".$parteE."','".$parteEB."','".$_SESSION['usuario']."','".$docum."',GETDATE())";
    $con->query($insert); 
    
    $oidp = "SELECT SCOPE_IDENTITY () oid";
    $oidpi = $con->query($oidp);
    
    while ($resp = mssql_fetch_array($oidpi)){
        $oid = $resp['oid'];
    }
    while($i < $n ){
        
        $listas['oid'.$i] = $lista[$i];
        $insert2 = "insert into cipres (ciplantilla,ciprespe)values('".$oid."','".$lista[$i]."')";
        $con->query($insert2);
        $i++;
    }
     echo json_encode($listas);
     
    $con->cerrarConexion();
    
 
?>